package au.edu.jcu.it.numberguess;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;
import android.widget.ViewFlipper;

public class HighScoresActivity extends Activity implements OnTouchListener {
	private List<HighScore> hsBeatClock;
	private List<HighScore> hsTimeTrial;
	private List<HighScore> hsAppGuess;
	private HighScoresOpenHelper highScoresOpenHelper;
	private ViewFlipper viewFlipper;
	ListView listView1, listView2, listView3;

	private float lastX;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_high_scores);
		// set home button enable
		ActionBar actionBar = getActionBar();
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
		highScoresOpenHelper = new HighScoresOpenHelper(this);
		hsBeatClock = new ArrayList<HighScore>();
		hsAppGuess = new ArrayList<HighScore>();
		hsTimeTrial = new ArrayList<HighScore>();
		hsBeatClock = fillData("BeatClock");
		hsTimeTrial = fillData("TimeTrial");
		hsAppGuess = fillData("AppGuess");
		listView1 = (ListView) findViewById(R.id.listView1);
		ListAdapter customAdapter = new ListAdapter(this, R.layout.listitem,
				hsBeatClock);
		listView1.setAdapter(customAdapter);
		listView1.setOnTouchListener(this);

		listView2 = (ListView) findViewById(R.id.listView2);
		ListAdapter customAdapter1 = new ListAdapter(this, R.layout.listitem,
				hsTimeTrial);
		listView2.setAdapter(customAdapter1);
		listView2.setOnTouchListener(this);

		listView3 = (ListView) findViewById(R.id.listView3);
		ListAdapter customAdapter2 = new ListAdapter(this, R.layout.listitem,
				hsAppGuess);
		listView3.setAdapter(customAdapter2);
		listView3.setOnTouchListener(this);
	}

	public List<HighScore> fillData(String tableName) {
		List<HighScore> list = new ArrayList<HighScore>();
		SQLiteDatabase db = highScoresOpenHelper.getReadableDatabase();
		Cursor cursor = db.query(true, tableName, null, null, null, null, null,
				"score" + " DESC", null);
		int count = 1;
		if (tableName.trim().equals("AppGuess")) {

			while (cursor.moveToNext()) {
				// String ID = cursor.getString(0);
				String ID = String.valueOf(count);
				String name = cursor.getString(1);
				String score = cursor.getString(2);
				String when = cursor.getString(3);
				list.add(new HighScore(ID, name, score, when));
				count += 1;
			}
		} else {
			while (cursor.moveToNext()) {
				// String ID = cursor.getString(0);
				String ID = String.valueOf(count);
				String name = cursor.getString(1);
				int score = cursor.getInt(2);
				String when = cursor.getString(3);
				list.add(new HighScore(ID, name, String.valueOf(score), when));
				count += 1;
			}
		}
		return list;

	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		// when user first touches the screen to swap
		case MotionEvent.ACTION_DOWN: {
			lastX = ev.getX();
			break;
		}
		case MotionEvent.ACTION_UP: {
			float currentX = ev.getX();

			// if left to right swipe on screen
			if (lastX < currentX) {
				// If no more View/Child to flip
				if (viewFlipper.getDisplayedChild() == 0)
					break;

				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Left and current Screen
				// will go OUT from Right
				viewFlipper.setInAnimation(this, R.anim.in_from_left);
				viewFlipper.setOutAnimation(this, R.anim.out_to_right);
				// Show the next Screen
				viewFlipper.showNext();
			}

			// if right to left swipe on screen
			if (lastX > currentX) {
				if (viewFlipper.getDisplayedChild() == 1)
					break;
				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Right and current Screen
				// will go OUT from Left
				viewFlipper.setInAnimation(this, R.anim.in_from_right);
				viewFlipper.setOutAnimation(this, R.anim.out_to_left);
				// Show The Previous Screen
				viewFlipper.showPrevious();
			}
			break;
		}
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.high_scores, menu);
		return true;
	}

	public boolean onTouch(View v, MotionEvent ev) {
		switch (ev.getAction()) {
		// when user first touches the screen to swap
		case MotionEvent.ACTION_DOWN: {
			lastX = ev.getX();
			break;
		}
		case MotionEvent.ACTION_UP: {
			float currentX = ev.getX();

			// if left to right swipe on screen
			if (lastX < currentX) {
				// If no more View/Child to flip
				if (viewFlipper.getDisplayedChild() == 0)
					break;

				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Left and current Screen
				// will go OUT from Right
				viewFlipper.setInAnimation(this, R.anim.in_from_left);
				viewFlipper.setOutAnimation(this, R.anim.out_to_right);
				// Show the next Screen
				viewFlipper.showNext();

			}

			// if right to left swipe on screen
			if (lastX > currentX) {
				if (viewFlipper.getDisplayedChild() == 1)
					break;
				// set the required Animation type to ViewFlipper
				// The Next screen will come in form Right and current Screen
				// will go OUT from Left
				viewFlipper.setInAnimation(this, R.anim.in_from_right);
				viewFlipper.setOutAnimation(this, R.anim.out_to_left);
				// Show The Previous Screen
				viewFlipper.showPrevious();
			}
			break;
		}
		}
		return true;

	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}
}
